package com.tianci.appstore.ui.list.uninstall;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.coocaa.x.framework.app.CoocaaApplication;
import com.coocaa.x.framework.utils.LogUtils;
import com.tianci.appstore.R;
import com.tianci.appstore.controller.manager.DeleteManagerController;
import com.tianci.appstore.ui.SkinManager;
import com.tianci.appstore.ui.data.SkyAppBaseData;
import com.tianci.appstore.ui.data.SkyListAppData;
import com.tianci.appstore.ui.data.SkyListAppData.ListType;
import com.tianci.appstore.ui.dialog.ConfirmProcessDialog;
import com.tianci.appstore.ui.list.ListBlock;
import com.tianci.appstore.ui.list.ListFlipper;
import com.tianci.appstore.ui.list.ListUninstallBlock;
import com.tianci.appstore.ui.list.manage.ManageBaseListView;
import com.tianci.appstore.ui.list.manage.ManageListController.SwitchFocusListener;
import com.tianci.appstore.ui.list.manage.MultiSelectUIListener;
import com.tianci.appstore.ui.util.UICommon;
import com.tianci.appstore.ui.widget.ColorButton;
import com.tianci.appstore.ui.widget.OnButtonClickListener;
import com.tianci.appstore.ui.widget.SkyCustomLayout;
import com.tianci.appstore.ui.widget.TipToast;
import com.tianci.appstore.utils.SkyConstants;

import java.util.ArrayList;
import java.util.List;

public class UninstallListView extends ManageBaseListView implements UninstallListDataListener,
        MultiSelectUIListener, OnClickListener, OnButtonClickListener, SwitchFocusListener {
    private LinearLayout btnLayout;
    private Button selectButton;
    private Button cancelButton;
    private Button uninstallButton;
    private boolean inSelectMode = false;
    private TextView countSizeText;

    private static final int UPDATE_SIZE = 6;
    private static final int UPDATE_UNINSTALL_RESULT = 7;
    private static final int HIDE_UNINSTALL_DLG = 8;
    private static final int UPDATE_UNINSTALL_STATUS = 9;
    private static final int UNINSTALL_START = 10;
    private static final int UNINSTALL_END = 11;
    private static final int SELECT_BTN_ID = 101;
    private static final int CANCEL_BTN_ID = 102;
    private static final int UNINSTALL_BTN_ID = 103;

    private LinearLayout.LayoutParams btnParams;

    boolean canInstallSilent = false;

    private List<SkyListAppData> dataList;
    private List<SkyListAppData> selList;

    private StringBuffer dialogTitleStr;

    public enum UninstallStatus{
        NOUNINSTALL,
        WAITUNINSTALL,
        UNINSTALLING,
        UNINSTALLED
    }

    public UninstallListView(Context context) {
        super(context, ListType.UNINSTALL_APP_LIST);
        // TODO Auto-generated constructor stub
        mController.setMultiSelectUIListener(this);
        DeleteManagerController.getInstance().setUninstallListDataListener(this);
        new Thread(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                try {
                    canInstallSilent = !CoocaaApplication.installWithPackageInstaller();
                    LogUtils.d("uninstall", "initTopRightLayout  canInstallSilent:" + canInstallSilent);
                    mainHandler.sendEmptyMessage(SLIENCE);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                DeleteManagerController.getInstance().refreshDeleteManagerList(null);
            }
        }).start();
        DeleteManagerController.getInstance().init();
        setLoadingVisible(true);
        mController.setSwitchFocusListener(this);
    }

    private void initTopRightLayout() {
        LinearLayout rightTopLayout = new LinearLayout(mContext);
        rightTopLayout.setGravity(Gravity.RIGHT | Gravity.CENTER_VERTICAL);

        countSizeText = UICommon.newTextView(mContext, rightTopTextSize, R.color.btnColor);
        countSizeText.setPadding(SkyConstants.Div(20), 0, SkyConstants.Div(20), 0);
        rightTopLayout.addView(countSizeText);
        if (canInstallSilent) {
//            ImageView btnTopImage = new ImageView(mContext);
//            SkinManager.setBackground(btnTopImage, R.drawable.pic_line);
//            LayoutParams dividerParams = new LayoutParams(LayoutParams.WRAP_CONTENT,
//                    LayoutParams.WRAP_CONTENT, Gravity.TOP | Gravity.RIGHT);
//            dividerParams.setMargins(0, SkyConstants.Div(86), SkyConstants.Div(94), 0);
//            this.addView(btnTopImage, dividerParams);

            btnLayout = new LinearLayout(mContext);
            // btnLayout.setPadding(SkyConstants.Div(22), 0, SkyConstants.Div(22), 0);
            rightTopLayout.addView(btnLayout, new LinearLayout.LayoutParams(
                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

            btnParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT);
            // btnParams.setMargins(SkyConstants.Div(10), 0, SkyConstants.Div(10), 0);
            btnParams.weight = 1;
            selectButton = newButton(SELECT_BTN_ID, R.string.batch_uninstall);
            btnLayout.addView(selectButton, btnParams);

            cancelButton = newButton(CANCEL_BTN_ID, R.string.cancel);
            cancelButton.setOnKeyListener(keyListener);
            uninstallButton = newButton(UNINSTALL_BTN_ID, R.string.uninstall);
            uninstallButton.setOnKeyListener(keyListener);
            uninstallButton.setOnFocusChangeListener(new OnFocusChangeListener() {

                @Override
                public void onFocusChange(View arg0, boolean hasFocus) {
                    // TODO Auto-generated method stub
                    if (hasFocus)
                        cancelButton.setFocusable(true);
                }
            });
            // rightTopLayout.setPadding(0, 0, SkyConstants.Div(27), 0);
        }
        // else
        // rightTopLayout.setPadding(0, 0, 0, 0);
        rightTopLayout.setPadding(0, 0, SkyConstants.Div(27), 0);

        LayoutParams topRightParams = new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT, Gravity.TOP | Gravity.RIGHT);
        topRightParams.setMargins(0, SkyConstants.Div(87), SkyConstants.Div(75), 0);
        this.addView(rightTopLayout, topRightParams);
    }

    private OnKeyListener keyListener = new OnKeyListener() {
        @Override
        public boolean onKey(View arg0, int keyCode, KeyEvent event) {
            // TODO Auto-generated method stub
            if (event.getAction() == KeyEvent.ACTION_UP) {
                if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
                    if (uninstallButton.isEnabled())
                        cancelButton.setFocusable(false);
                }
            }
            return false;
        }
    };

    private Button newButton(int btnId, int strId) {
        Button btn = UICommon.newButton(mContext, rightTopTextSize, R.color.btnColor);
        SkinManager.setBackground(btn, R.drawable.search_selector);
        // btn.setPadding(SkyConstants.Div(27), 0, SkyConstants.Div(27), SkyConstants.Div(6));
        btn.setText(strId);
        btn.setId(btnId);
        btn.setOnClickListener(this);
        return btn;
    }

    private static final int SLIENCE = 2;

    private Handler mainHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case UPDATE_LIST:
                    refreshListData(dataList, selList);
                    break;
                case UPDATE_SIZE:
                    refreshFreeSize(msg.getData().getFloat("internalSize"),
                            msg.getData().getFloat("externalSize"));
                    break;
                case UPDATE_UNINSTALL_RESULT:
                    refreshUninstallResult(msg.arg1, (List<SkyListAppData>) msg.obj);
                    break;
                case HIDE_UNINSTALL_DLG:
                    if (dialog != null)
                        dialog.hide();
                    LogUtils.d("", "sunny resume 3");
                    break;
                case SLIENCE:
                    initTopRightLayout();
                    break;
                case UPDATE_UNINSTALL_STATUS:
                    silentUninstall();
                    setSelectMode(false);
                    break;
                case UNINSTALL_START:
                    uninstallStart((String) msg.obj);
                    break;
                case UNINSTALL_END:
                    uninstallEnd((String) msg.obj, msg.arg1);
                    break;
            }
            super.handleMessage(msg);
        }
    };

    @Override
    public void onUpdateCountAndSize(float internalSize, float externalSize) {
        // TODO Auto-generated method stub
        Message msg = mainHandler.obtainMessage(UPDATE_SIZE);
        Bundle bundle = new Bundle();
        bundle.putFloat("internalSize", internalSize);
        bundle.putFloat("externalSize", externalSize);
        msg.setData(bundle);
        mainHandler.sendMessage(msg);
    }

    private void refreshFreeSize(float internalSize, float externalSize) {
        // if(appCount == 0)
        // {
        // setEmptyTipVisible(true);
        // }
        // else{
        // setEmptyTipVisible(false);
        // }
        if (externalSize < 0) {
            String str = String.format(getResources().getString(R.string.free_size_tip),
                    UICommon.sizeToString(internalSize));
            countSizeText.setText(str);
        } else {
            String str = String.format(getResources().getString(R.string.free_size_tip) + "  "
                            + getResources().getString(R.string.free_size_sdcard),
                    UICommon.sizeToString(internalSize), UICommon.sizeToString(externalSize));
            countSizeText.setText(str);
        }
    }

    @Override
    public void resume() {
        // TODO Auto-generated method stub
        int uninstallCount = DeleteManagerController.getInstance().getWaitUninstallList();
        if (uninstallCount == 0) {
            if (dialog != null) {
                mainHandler.sendEmptyMessageDelayed(HIDE_UNINSTALL_DLG, 1500);
            }
        }
    }

    @Override
    public void onUninstallListLoaded(List<SkyListAppData> dataList, List<SkyListAppData> selList) {
        // TODO Auto-generated method stub
        this.dataList = dataList;
        this.selList = selList;
        Message msg = mainHandler.obtainMessage(UPDATE_LIST);
        mainHandler.sendMessage(msg);
    }

    @Override
    public void onUninstallStart(String pkg) {
        LogUtils.d("uninstall", "uninstall onUninstallStart pkg:" + pkg);

        Message msg = mainHandler.obtainMessage(UNINSTALL_START);
        msg.obj = pkg;
        mainHandler.sendMessage(msg);

    }

    @Override
    public void onUninstallFinished(int success, String pkg) {
        LogUtils.d("uninstall", "uninstall onUninstallFinished pkg:" + pkg + "; " + success);
        Message msg = mainHandler.obtainMessage(UNINSTALL_END);
        msg.obj = pkg;
        msg.arg1 = success;
        mainHandler.sendMessage(msg);

    }

    private void uninstallStart(String pkg){
        if(!canInstallSilent)
            return;
        for (int i = 0; i < ListFlipper.VIEW_PAGE_COUNT; i++) {
            SkyCustomLayout child = (SkyCustomLayout) listFlipper.getChildAt(i);
            for (View view : child.getChildViews()) {
                ListUninstallBlock block = (ListUninstallBlock) view;
                SkyAppBaseData baseData = block.getData();
                if (baseData != null) {
                    SkyListAppData apkData = (SkyListAppData) baseData;
                    LogUtils.d("uninstall", "uninstall onUninstallStart pkg:" + pkg + "; " + apkData);
                    if (pkg != null && apkData != null && apkData.getPkgName() != null && pkg.equals(apkData.getPkgName()))
                    {
                        LogUtils.d("uninstall", "uninstall onUninstallStart pkg 2:" + pkg);
                        block.updateStatus(true, UninstallStatus.UNINSTALLING);
                    }
                }
            }
        }
    }

    private void uninstallEnd(String pkg, int success) {
        if(!canInstallSilent)
            return;
        if (success != 0) {
            for (int i = 0; i < ListFlipper.VIEW_PAGE_COUNT; i++) {
                SkyCustomLayout child = (SkyCustomLayout) listFlipper.getChildAt(i);
                for (View view : child.getChildViews()) {
                    ListUninstallBlock block = (ListUninstallBlock) view;
                    SkyAppBaseData baseData = block.getData();
                    if (baseData != null) {
                        SkyListAppData apkData = (SkyListAppData) baseData;
                        if (apkData != null && !apkData.getPkgName().equals("") && pkg.equals(apkData.getPkgName())) {
                            block.updateStatus(false, UninstallStatus.NOUNINSTALL);
                        }
                    }
                }
            }
        }
        List<String> uninstallList = DeleteManagerController.getInstance().getUninstallList();
        if(uninstallList != null && uninstallList.size() > 0){
            setBtnEnabled(selectButton, false);
        }else{
            setBtnEnabled(selectButton, true);
        }

    }

    private void refreshListData(List<SkyListAppData> dataList, List<SkyListAppData> selList) {
        if (dataList == null || dataList.size() == 0) {
            setEmptyTipVisible(true, mContext.getResources().getString(R.string.no_uninstall_task));
            if (canInstallSilent) {
                setBtnEnabled(selectButton, false);
                btnLayout.removeAllViews();
                btnLayout.addView(selectButton, btnParams);
            }
            return;
        }

        setLoadingVisible(false);
        setEmptyTipVisible(false, null);
        if (selList == null || selList.size() == 0) {
            setSelectMode(false);
        } else {
            setSelectMode(true);
            mController.setSelectedDataList(selList);
        }

        if (lastFocusPageIndex != -1 && lastFocusIndex != -1) {
            mController.setDataList(dataList, lastFocusPageIndex, lastFocusIndex);
            lastFocusPageIndex = -1;
            lastFocusIndex = -1;
        } else {
            mController.setDataList(dataList);
            resetFocus();
        }
        LogUtils.d("uninstall","sunny uninstall silentUninstall refreshListData:"+dataList.size());
        silentUninstall();
    }

    @Override
    public void setEmptyTipVisible(boolean visible, String tipStr) {
        super.setEmptyTipVisible(visible, tipStr);

        if (btnLayout != null)
            setBtnEnabled(selectButton, !visible);
    }

    private void silentUninstall(){
        if(!canInstallSilent)
            return;
        boolean isUninstalling = false;
        List<String> uninstallList = DeleteManagerController.getInstance().getUninstallList();
        if(uninstallList != null && uninstallList.size() > 0){
            setBtnEnabled(selectButton, false);
            LogUtils.d("uninstall","sunny uninstall silentUninstall 00");
            isUninstalling = true;
        }else{
            setBtnEnabled(selectButton, true);
            LogUtils.d("uninstall","sunny uninstall silentUninstall 11");
        }
        for (int i = 0; i < ListFlipper.VIEW_PAGE_COUNT; i++) {
            SkyCustomLayout child = (SkyCustomLayout) listFlipper.getChildAt(i);
            for (View view : child.getChildViews()) {
                ListUninstallBlock block = (ListUninstallBlock) view;
                block.updateStatus(false, UninstallStatus.NOUNINSTALL);
                if(isUninstalling){
                    LogUtils.d("uninstall","sunny uninstall silentUninstall:"+uninstallList.size()+"; "+uninstallList.get(0));
                    SkyAppBaseData baseData = block.getData();
                    if (baseData != null) {
                        SkyListAppData apkData = (SkyListAppData) baseData;
                        for (String pkg : uninstallList) {
                            if (pkg !=null && apkData != null && apkData.getPkgName() != null && pkg.equals(apkData.getPkgName())) {
                                block.updateStatus(true, UninstallStatus.WAITUNINSTALL);
                            }
                        }
                        String installPkg = uninstallList.get(0);
                        if (installPkg.equals(apkData.getPkgName())) {
                            block.updateStatus(true, UninstallStatus.UNINSTALLING);
                        }
                    }
                }
            }
        }
    }

    private void setSelectMode(boolean selMode) {
        if (this.inSelectMode == selMode)
            return;
        this.inSelectMode = selMode;
        if (selMode) {
            if (btnLayout != null) {
                btnLayout.removeAllViews();
                btnLayout.addView(cancelButton, btnParams);
                btnLayout.addView(uninstallButton, btnParams);
                // cancelButton.setFocusable(false);
                setBtnEnabled(uninstallButton, false);
            }

            // ListBlock.setSelectMode(inSelectMode);
            setInSelectMode(selMode);
            for (int i = 0; i < listFlipper.getChildCount(); i++) {
                SkyCustomLayout child = (SkyCustomLayout) listFlipper.getChildAt(i);
                for (int j = 0; j < child.getChildCount(); j++) {
                    ListBlock block = (ListBlock) child.getChildAt(j);
                    block.refreshSelectState();
                }
            }
            mController.clearSelectedDataList();
            resetFocus();
        } else {
            if (btnLayout != null) {
                btnLayout.removeAllViews();
                btnLayout.addView(selectButton, btnParams);
            }
            mController.clearSelectedDataList();
            // ListBlock.setSelectMode(false);
            setInSelectMode(false);
            for (int i = 0; i < listFlipper.getChildCount(); i++) {
                SkyCustomLayout child = (SkyCustomLayout) listFlipper.getChildAt(i);
                for (int j = 0; j < child.getChildCount(); j++) {
                    ListBlock block = (ListBlock) child.getChildAt(j);
                    block.refreshSelectState();
                }
            }
            resetFocus();
        }
    }

    @Override
    public void OnSelectCountChanged(int count) {
        // TODO Auto-generated method stub
        if (inSelectMode) {
            if (btnLayout != null)
                setBtnEnabled(uninstallButton, count != 0);
        }
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
            case SELECT_BTN_ID:
                if (!inSelectMode)
                    setSelectMode(!inSelectMode);
                break;
            case CANCEL_BTN_ID:
                if (inSelectMode) {
                    setSelectMode(!inSelectMode);
                    resetFocus();
                }
                break;
            case UNINSTALL_BTN_ID: {
                List<SkyListAppData> selectList = mController.getSelectedDataList();
                int selSize = selectList.size();
                uninstallCount = selSize;
//                initUninstallDialog();
                lastFocusIndex = -1;
                lastFocusPageIndex = -1;
                if (uninstallCount > 1) {
                    DeleteManagerController.getInstance().uninstallAppList(
                            mController.getSelectedDataList());
                } else {
//                    DeleteManagerController.getInstance().uninstallAppList(selectData);
                    if(selectList != null && selectList.size() > 0){
                        DeleteManagerController.getInstance().uninstallAppList(selectList);
                    }else if(selectData != null && selectData.size() > 0){
                        DeleteManagerController.getInstance().uninstallAppList(selectData);
                    }
                }
                Message msg = mainHandler.obtainMessage(UPDATE_UNINSTALL_STATUS);
                mainHandler.sendMessageDelayed(msg, 1000);
            }
            break;
        }
    }

    @Override
    public void destroyController() {
        // TODO Auto-generated method stub
        DeleteManagerController.getInstance().destroy();
        if (dialog != null) {
            dialog.onDestory();
        }
        super.destroyController();
    }

    @Override
    public boolean processKeyDown(int keyCode) {
        // TODO Auto-generated method stub
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (inSelectMode) {
                setSelectMode(!inSelectMode);
                return true;
            }
        }
        return false;
    }

    // private UninstallDialog dialog;
    private ConfirmProcessDialog dialog;
    private int uninstallCount = 0;
    private List<SkyListAppData> selectData = new ArrayList<SkyListAppData>();

    // private int crtUninstallIndex = 0;

    @Override
    public void onBlockClicked(View v, SkyListAppData data, int index) {
        // TODO Auto-generated method stub
        lastFocusPageIndex = mController.getCurrentPageIndex();
        lastFocusIndex = index;
        if (selectData.size() > 0) {
            selectData.clear();
        }
        if(data == null)
            return;
        if (canInstallSilent) {
            selectData.add(data);
            List<String> uninstallList = DeleteManagerController.getInstance().getUninstallList();
            String dataPkg = data.getPkgName();
            int listSize = uninstallList.size();
            if(uninstallList != null && uninstallList.size() > 0){
                for(int i=0; i<listSize; i++){
                    String pkg = uninstallList.get(i);
                    if(pkg != null && dataPkg != null && pkg.equals(dataPkg)){
                        String uninstallingStr;
                        if(i==0){
                            uninstallingStr = String.format(
                                    mContext.getResources().getString(R.string.uninstalling_app),
                                    data.getTitle());
                        }else{
                            uninstallingStr = mContext.getString(R.string.waituninstall_app);
                        }
                        TipToast.getInstance(mContext).showTipToast(uninstallingStr);
                        return;
                    }
                }
            }
            uninstallCount = 1;
            initUninstallDialog();
            dialog.setIconTitle(data, data.getTitle());
            dialog.setContent(mContext.getString(R.string.uninstall_tip));
            dialog.setBtnFocusTag(1);
            dialog.show();
        } else {
            DeleteManagerController.getInstance().uninstallApp(data.getPkgName());
        }
    }

    private void initUninstallDialog() {
        if (dialog == null) {
            dialog = new ConfirmProcessDialog(mContext);
            dialog.setButtonClickListener(this);
            dialog.addButton(new ColorButton(mContext, R.string.cancel_uninstall,
                    R.drawable.btn_gray, R.drawable.btn_gray_focus), 0, false);
            dialog.addButton(new ColorButton(mContext, R.string.uninstall, R.drawable.btn_green,
                    R.drawable.btn_green_focus), 1, true);
        } else {
            dialog.removeAllBtns();
            dialog.addButton(new ColorButton(mContext, R.string.cancel_uninstall,
                    R.drawable.btn_gray, R.drawable.btn_gray_focus), 0, false);
            dialog.addButton(new ColorButton(mContext, R.string.uninstall, R.drawable.btn_green,
                    R.drawable.btn_green_focus), 1, true);
            dialog.initStatus();
        }
    }

    // 弹出框中的button点击事件
    @Override
    public void onClick(int index) {
        // TODO Auto-generated method stub
        if (index == 0) {
            dialog.hide();
        } else if (index == 1) {
            String uninstallingStr;
            if (uninstallCount > 1) {
                DeleteManagerController.getInstance().uninstallAppList(
                        mController.getSelectedDataList());
                List<SkyListAppData> selectList = mController.getSelectedDataList();
                int selSize = selectList.size();
                uninstallingStr = String.format(
                        mContext.getResources().getString(R.string.uninstalling_xx_apps),
                        dialogTitleStr, selSize);
            } else {
                if(selectData != null && selectData.size() > 0){
                    DeleteManagerController.getInstance().uninstallAppList(selectData);
                }else if(mController.getSelectedDataList() != null && mController.getSelectedDataList().size() > 0){
                    DeleteManagerController.getInstance().uninstallAppList(mController.getSelectedDataList());
                }
//                DeleteManagerController.getInstance().uninstallAppList(selectData);
                Message msg = mainHandler.obtainMessage(UPDATE_UNINSTALL_STATUS);
                mainHandler.sendMessageDelayed(msg, 1000);
                if (dialog != null)
                    dialog.hide();
//                uninstallingStr = mContext.getString(R.string.uninstalling_xx_app);
            }
//            if (dialog != null)
//                dialog.enterWaitState(uninstallingStr);
        } else if (index == 2) {
            if (dialog != null)
                dialog.hide();
        }
    }

   /* @Override
    public void onUninstallFinished(int successCount, List<SkyListAppData> failList) {
        Message msg = mainHandler.obtainMessage(UPDATE_UNINSTALL_RESULT);
        msg.arg1 = successCount;
        msg.obj = failList;
        mainHandler.sendMessage(msg);
    }*/

    private void refreshUninstallResult(int successCount, List<SkyListAppData> failList) {
        // TODO Auto-generated method stub
        String contentStr = "";
        if (successCount != -1 && successCount < uninstallCount && failList != null) {
            StringBuffer failTitleListStr = new StringBuffer();
            int i = 0;
            for (SkyListAppData data : failList) {
                if (i < failList.size() - 1)
                    failTitleListStr.append(data.getTitle() + ",");
                else
                    failTitleListStr.append(data.getTitle());
                i++;
            }
            contentStr = String.format(
                    mContext.getResources().getString(R.string.uninstall_result), successCount,
                    uninstallCount - successCount, failTitleListStr);
        } else {
            if(successCount==-1) {
                contentStr = String.format(mContext.getString(R.string.uninstall_failed), "");
            }else {
                if (uninstallCount == 1)
                    contentStr = String.format(mContext.getString(R.string.uninstall_finish), "");
                else {
                    contentStr = String.format(mContext.getString(R.string.uninstall_finish),
                            dialogTitleStr);
                }
            }
        }
        if (dialog != null) {
            dialog.enterResultState(contentStr);
            if (successCount < uninstallCount) {
                dialog.removeAllBtns();
                dialog.addButton(new ColorButton(mContext, R.string.confirm, R.drawable.btn_green,
                        R.drawable.btn_green_focus), 2, true);
            } else {
                mainHandler.sendEmptyMessageDelayed(HIDE_UNINSTALL_DLG, 1500);
            }
        }
    }

    // 保证在选择状态时，选好了应用，按“上”键会首先聚焦至“卸载”按钮，而非“取消”按钮
    @Override
    public boolean switchFocus(int direction) {
        // TODO Auto-generated method stub
        if (direction == KeyEvent.KEYCODE_DPAD_UP) {
            if (uninstallButton != null && uninstallButton.isEnabled()
                    && uninstallButton.getParent() != null) {
                uninstallButton.requestFocus();
                return true;
            }
        }
        return false;
    }

    @Override
    public void onItemAudioMatch(SkyAppBaseData data, int index) {
        selectData.clear();
        selectData.add((SkyListAppData) data);
        DeleteManagerController.getInstance().uninstallAppList(selectData);
    }

}